import 'package:dio/dio.dart';
import 'package:mall/core/models/store_total.dart';
import 'package:mall/core/services/api.dart';
import 'package:mall/core/providers/view_model.dart';

class StoreManageModel extends ViewModel {
  final Api api;
  final int storeId;
  String _errorMessage;
  StoreTotal _storeTotal;

  StoreManageModel(this.api, this.storeId) : super() {
    _initData();
  }

  String get errorMessage => _errorMessage;

  StoreTotal get storeTotal => _storeTotal;

  Future<ViewState> _initData() async {
    viewState = ViewState.busy;
    try {
      _storeTotal = await api.storeTotal(storeId);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
